﻿#include "initwidget.h"
#include "ui_initwidget.h"
#include <QMessageBox>
#include <QDateTime>
#include <QMovie>
#include <QTextStream>
#include <QTimer>

InitWidget::InitWidget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::InitWidget)
{
    ui->setupUi(this);
    setWindowIcon(QIcon(":icon/app.ico"));
    setWindowFlags(Qt::FramelessWindowHint | Qt::Window | Qt::WindowMinimizeButtonHint);
    connect(ui->btnMin,&QPushButton::clicked,this,&InitWidget::windowMin);
    connect(ui->btnMax,&QPushButton::clicked,this,&InitWidget::windowMax);
    connect(ui->btnClose,&QPushButton::clicked,this,&InitWidget::windowClose);
    initWindow();
    loadQssFile();
    ui->btnMax->hide();

    QTimer *timer = new QTimer(this);
    connect(timer, SIGNAL(timeout()), this, SLOT(updateTime()));
    timer->start(1000);
    windowMax();
}

InitWidget::~InitWidget()
{
    delete ui;
}

void InitWidget::initWindow()
{
    isMaxWin=true;
    ui->btnMax->setIcon(QIcon(":/img/max.png"));
    ui->btnMin->setIcon(QIcon(":/img/minimize.png"));
    ui->btnClose->setIcon(QIcon(":/img/close.png"));

    QMovie *movie = new QMovie(":/img/logo.gif");
    QSize size(ui->textLogo->width(),ui->textLogo->height());
    movie->setScaledSize(size);

    movie->start();         // 2. 启动动画
    ui->textLogo->setMovie(movie); // 1. 设置要显示的 GIF 动画图片
    ui->textLogo->show();


}

void InitWidget::loadQssFile()
{
    QFile file(":/qss/initForm.qss");
    if (file.open(QFile::ReadOnly))
    {
        QTextStream filetext(&file);
        QString stylesheet = filetext.readAll();
        this->setStyleSheet(stylesheet);
        file.close();
    }
}

void InitWidget::addWidget(QWidget *widget)
{
    ui->horizontalLayout->insertWidget(1,widget);
}

void InitWidget::mousePressEvent(QMouseEvent *event)
{
    if(event->button() == Qt::LeftButton)
    {
        isPressed=true;
        curPos = event->pos();
    }
}

void InitWidget::mouseMoveEvent(QMouseEvent *event)
{
    if(isPressed)
    {
        //当窗口最大化或最小化时也不进行触发
        if(InitWidget::isMaximized() || InitWidget::isMinimized())
        {
            return;
        }
        else
        {
            //当在按钮之类需要鼠标操作的地方不进行触发(防误触)
            if (ui->btnClose->underMouse()||ui->btnMax->underMouse()||ui->btnMin->underMouse())
            {
                return;
            }
            if(ui->titleWidget->underMouse())
            {
                this->move(event->pos()-curPos+this->pos());    //窗口移动
            }
        }
    }
    event->accept();
}

void InitWidget::mouseReleaseEvent(QMouseEvent *event)
{
    isPressed = false;
}

void InitWidget::mouseDoubleClickEvent(QMouseEvent *event)
{
    Q_UNUSED(event)

    if(ui->titleWidget->underMouse())
    {
        isMaxWin=!isMaxWin;
        if(isMaxWin)
        {

            this->showNormal();
            ui->btnMax->setIcon(QIcon(":/img/max.png"));
        }
        else
        {
            this->showMaximized();
            ui->btnMax->setIcon(QIcon(":/img/mini.png"));
        }
    }
}



void InitWidget::windowMin()
{
    this->showMinimized();
}

void InitWidget::windowMax()
{
    isMaxWin=!isMaxWin;
    if(isMaxWin)
    {
        this->showNormal();
        ui->btnMax->setIcon(QIcon(":/img/max.png"));
    }
    else
    {
        this->showMaximized();
        ui->btnMax->setIcon(QIcon(":/img/mini.png"));
    }
}

void InitWidget::windowClose()
{
    if(!QMessageBox::information(this, tr("info"), tr("Exit the App!"), "Yes", "No")){
        emit appQuit();
        qApp->exit();
    }
}

void InitWidget::show()
{
    this->setVisible(true);
}
#include <AppKey/appkey.h>
void InitWidget::updateTime()
{
    QDateTime time=QDateTime::currentDateTime();
    QString timeText = time.toString("yyyy-MM-dd hh:mm:ss dddd");
    ui->textTime->setText(timeText);
    QString deadLine = QString(tr("%1 day")).arg(AppKey::Instance()->getDeadline());
    ui->textDeadline->setText(deadLine);
}

